package graph.day13;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Leetcode_1365 {

    public int[] smallerNumbersThanCurrent(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = Arrays.copyOf(nums, nums.length);
        bubbleSort(res);
        for (int i = 0; i < res.length; i++) {
            if (!map.containsKey(res[i])) { // 遇到了相同的数字，那么不需要更新该 number 的情况
                map.put(res[i], i);
            }
        }

        for (int i = 0; i < nums.length; i++) {
            res[i] = map.get(nums[i]);
        }

        return res;
    }

    public void bubbleSort(int[] nums) {
        int j = nums.length - 1;
        do {
            int x = 0;
            for (int i = 0; i < j; i++) {
                if (nums[i] > nums[i + 1]) {
                    int temp = nums[i + 1];
                    nums[i + 1] = nums[i];
                    nums[i] = temp;
                    x = i;
                }
            }
            j = x;
        } while (j != 0);
    }

}
